<?php

namespace App\Admin\Actions\NewsAction;

use App\Models\User;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use App\Supports\LinkedIn\Client;


class tiktokPost extends RowAction
{
    public $name = 'tiktok上传及发布视频';
    
    public function handle(Model $model)
    {
	    /**
	     * @var $model User
	     */
    	
    	/*$media_info =file_get_contents('https://xbreaking.oss-cn-beijing.aliyuncs.com/yuyin/b4c259c9e10759dd53bcb40ccc0aa0c0.mp4');
	    $user = User::find(1);

	    if($user->tiktok_access_token){


	    }else{



	    }*/

	   /* $url="https://www.tiktok.com/v2/auth/authorize/?client_key=sbawrw2yqiyidrxta1&response_type=code&scope=user.info.basic,video.upload&redirect_uri=https://api.xbreakingtechinsight.com/admin&disable_auto_auth=0";*/
	   $media_info =file_get_contents('https://xbreaking.oss-cn-beijing.aliyuncs.com/yuyin/b4c259c9e10759dd53bcb40ccc0aa0c0.mp4');

	  /* $code="_YkTBanto-ZQkyzJ-yKcDNaZBGwAwT1lytx6vjqDu5zqSmiUTsCMTgP4mvRfI8By_pUNYIgOuZpun7rzrC4y7C6StRqI4oILkG5baFJSqdmpKWcU4Ux8aPJ_aFyTZBKoLInIG2gpy75FvJgv0aSeTz90cLKyXoGXkLNfqPXt_PJBOMQ-yXzie8jFO3MeLI_d%2A1%216161.va";

	   $accessTokenData = [
	   	 'client_key'=>'sbawrw2yqiyidrxta1',
	   	 'client_secret'=>'AmzIGz5GyftwWSHWadhM9qg1IRJsxPFd',
	   	 'code'=>$code,
	   	 'grant_type'=>'authorization_code',
	   	 'redirect_uri'=>'https://api.xbreakingtechinsight.com/admin'
	   ];
	   $accessTokenUrl = "https://open.tiktokapis.com/v2/oauth/token/";

	   $accessTokenResult = $this->auth($accessTokenUrl,$accessTokenData);*/
	   $token ="act.WyQJMpjTM5WMg2NVQQIBIrWlXHExkYSXIAgRiyX06aPEE0ZzMD6Biwm1HiFw!6184.va";


	   $uploadUrl="https://open.tiktokapis.com/v2/post/publish/inbox/video/init/";

	    $chunks = mb_str_split($media_info,6000000);
	    $count = count($chunks);
	    if(strlen($chunks[$count-1]) < 6000000){

	    	$count = $count - 1;
	    }

	    //$this->auth($url,)
    	$data=[
    		"source_info"=>[
			        "source"=>"FILE_UPLOAD",
			        "video_size"=> strlen($media_info),
			        "chunk_size" => 6000000,
			        "total_chunk_count"=> $count,
			    ]
    	];

    	$res = $this->curl($uploadUrl,$data,$token);
    	dd($res);
    	
	    return $this->response()->success('发布成功')->refresh();
    }


    public function auth($url,$data){

		$curl = curl_init();
		$data =  http_build_query($data);


		curl_setopt_array($curl, array(
		   CURLOPT_URL => $url,
		   CURLOPT_RETURNTRANSFER => true,
		   CURLOPT_ENCODING => '',
		   CURLOPT_MAXREDIRS => 10,
		   CURLOPT_TIMEOUT => 0,
		   CURLOPT_FOLLOWLOCATION => true,
		   CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		   CURLOPT_CUSTOMREQUEST => 'POST',
		   CURLOPT_POSTFIELDS =>$data,
		   CURLOPT_HTTPHEADER => array(
		      'Content-Type: application/x-www-form-urlencoded',
		      'Cache-Control: no-cache'
		   ),
		));

	   $response = curl_exec($curl);

	   //dd($response);
	   return $response;
	}

    public function curl($url,$data,$token){

		$curl = curl_init();
		$data = json_encode($data);


		curl_setopt_array($curl, array(
		   CURLOPT_URL => $url,
		   CURLOPT_RETURNTRANSFER => true,
		   CURLOPT_ENCODING => '',
		   CURLOPT_MAXREDIRS => 10,
		   CURLOPT_TIMEOUT => 0,
		   CURLOPT_FOLLOWLOCATION => true,
		   CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		   CURLOPT_CUSTOMREQUEST => 'POST',
		   CURLOPT_POSTFIELDS =>$data,
		   CURLOPT_HTTPHEADER => array(
		      'Authorization: Bearer '.$token,
		      'Content-Type: application/json'
		   ),
		));

	   $response = curl_exec($curl);

	   //dd($response);
	   return $response;
	}
	
	public function dialog()
	{
		$this->confirm('确定发布到linkedin吗?');
	}
}
